package com.weex.app.extend;

import android.content.ContentValues;
import android.content.Context;
import android.os.Parcel;
import android.util.Base64;

import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import com.taobao.weex.common.WXModule;
import com.tencent.wcdb.Cursor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class WxWcdb extends WXModule {
    WxSQLiteOpenHelper opener;

    @JSMethod(uiThread=false)
    public void open(String name,int version,JSCallback onCreate,JSCallback onUpgrade){
        Context ctx = mWXSDKInstance.getContext();
        opener = new WxSQLiteOpenHelper(ctx,name,version,onCreate,onUpgrade);
    }

    @JSMethod(uiThread=false)
    public synchronized void close(){
        if(opener.db != null && opener.db.isOpen()){
            opener.db.close();
            opener.db = null;
        }
    }

    @JSMethod(uiThread = false)
    public void insert(String table, Map<String,Object> values, int conflictAlgorithm, JSCallback callback){
        opener.open();
        Parcel p = Parcel.obtain();
        p.writeMap(values);
        p.setDataPosition(0);

        long id = opener.db.insertWithOnConflict(
                table,
                null,
                ContentValues.CREATOR.createFromParcel(p),
                conflictAlgorithm
        );
        callback.invoke(id);
    }

    @JSMethod(uiThread = false)
    public void execute(String sql, JSCallback callback){
        opener.open();
        opener.db.execSQL(sql);
        callback.invoke(null);
    }

    @JSMethod(uiThread = false)
    public void execute(String sql,Object[] params, JSCallback callback){
        opener.open();
        opener.db.execSQL(sql, params);
        callback.invoke(null);
    }

    @JSMethod(uiThread = false)
    public void query(String sql,String[] params,JSCallback callback){
        Cursor c = opener.db.rawQuery(sql, params);

        List<Object[]> li = new ArrayList<Object[]>();
        if(c!=null) {
            int count = c.getColumnCount();

            int[] type_li = new int[count];

            for (int i = 0; i < count; i++) {
                type_li[i] = c.getType(i);
            }

            while (c.moveToNext()) {
                Object[] r = new Object[count];
                for (int i = 0; i < count; i++) {
                    switch (type_li[i]) {
                        case Cursor.FIELD_TYPE_INTEGER:
                            r[i] = c.getInt(i);
                            break;
                        case Cursor.FIELD_TYPE_FLOAT:
                            r[i] = c.getFloat(i);
                            break;
                        case Cursor.FIELD_TYPE_STRING:
                            r[i] = c.getString(i);
                            break;
                        case Cursor.FIELD_TYPE_BLOB:
                            r[i] = Base64.encodeToString(
                                    c.getBlob(i),
                                    Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP
                            );
                            break;
                    }
                }
                li.add(r);
            }
        }
        callback.invoke(li);
    }


/*

 execSQL()方法可以执行insert、delete、update和CREATE TABLE之类有更改行为的SQL

 db.execSQL("insert into person(name, age) values(?,?)", new Object[]{"传智播客", 4});


 rawQuery()方法可以执行select语句。

 */
}
